package com.jcl.recruit.basic.register.impl;

import com.jcl.core.constants.Constant;
import com.jcl.core.utils.DateUtils;
import com.jcl.recruit.basic.register.model.RegisterCorpResult;
import com.jcl.recruit.basic.register.vo.RegisterVo;
import com.jcl.recruit.core.constants.Constants;
import com.jcl.recruit.corp.dao.SubcompanyDao;
import com.jcl.recruit.corp.model.Subcompany;
import com.jcl.recruit.depart.dao.DepartmentDao;
import com.jcl.recruit.org.model.Department;
import com.jcl.recruit.user.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @ClassName CorpRecruitRegister
 * @Author yyliu
 * @Date 2018/12/17 10:42
 * @Version 1.0
 **/
@Component
public class CorpRecruitRegister {

    @Autowired
    private SubcompanyDao subcompanyDao;

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private UserRecruitRegister userRecruitRegister;

    private RegisterVo registerVo;

    public RegisterCorpResult register(String corpCode, String password) {
        RegisterCorpResult result = null;
        Subcompany subcompany = this.registerCorp(corpCode);
        if (null != subcompany) {
            result = new RegisterCorpResult();
            // 注册部门
            Department department = this.registerDepart(corpCode, subcompany.getId());
            // 注册用户，默认管理员
            User user = userRecruitRegister.registerUser(corpCode, department.getId(), password, true);
            // 更新企业添加人
            subcompanyDao.updateAddUser(corpCode, subcompany.getId(), user.getId());
            result.setUser(user);
            result.setDepartment(department);
            result.setSubcompany(subcompany);
        }
        return result;
    }

    public Subcompany registerCorp(String corpCode) {

        if (null != corpCode && null != registerVo) {
            Subcompany subcompany = new Subcompany();
            subcompany.setFullname(registerVo.getCorpName());
            subcompany.setShortname(registerVo.getCorpName());

            subcompany.setEmail(registerVo.getEmail());
            subcompany.setTelephone(registerVo.getTelephone());
            subcompany.setFullname(registerVo.getCorpName());

            // 试用版
            subcompany.setMealPackage(Constants.MealPackage.default_package);
            Date date = new Date();
            subcompany.setAbateDate(DateUtils.addDays(date, Constants.MealPackage.default_day));
            subcompany.setRegisterDate(date);
            subcompany.setAddDate(date);
            subcompanyDao.add(subcompany);
            return subcompany;
        }
        return null;
    }

    private Department registerDepart(String corpCode, Integer corpId) {
        Department department = new Department();
        department.setFullname(registerVo.getCorpName());
        department.setShortname(registerVo.getCorpName());
        department.setSubcompanyId(corpId);
        department.setCorpCode(corpCode);
        department.setSuperId(Constants.DepartmentInfo.ROOT_SUPER_ID);
        department.setCode(Constant.NUMBER_0 + "");
        department.setShoworder(Constant.NUMBER_1);
        departmentDao.add(department);
        return department;
    }

    public RegisterVo getRegisterVo() {
        return registerVo;
    }

    public void setRegisterVo(RegisterVo registerVo) {
        this.registerVo = registerVo;
    }
}
